package com.cet.lxf.collection.test08;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.TreeSet;

/**
 * @program: algorithm
 * @description: 使用Set实现消息去重
 * @author: 陈恩涛
 * @create: 2021-08-12 17:23
 **/
public class Main {
    public static void main(String[] args) {
        List<Message> received = List.of(
                new Message(1, "Hello!"),
                new Message(2, "发工资了吗？"),
                new Message(2, "发工资了吗？"),
                new Message(3, "去哪吃饭？"),
                new Message(3, "去哪吃饭？"),
                new Message(4, "Bye")
        );
        List<Message> displayMessages = process(received);
        for (Message message : displayMessages) {
            System.out.println(message.text);
        }
    }

    static List<Message> process(List<Message> received) {
        // TODO: 按sequence去除重复消息
        TreeSet<Message> treeSet = new TreeSet<>(Comparator.comparing(Message::getSequence));
        treeSet.addAll(received);
        return new ArrayList<>(treeSet);
    }
}

class Message {
    public final int sequence;
    public final String text;
    public Message(int sequence, String text) {
        this.sequence = sequence;
        this.text = text;
    }

    public int getSequence() {
        return sequence;
    }

    public String getText() {
        return text;
    }
}

